import { HttpException, HttpStatus } from '@nestjs/common';

import { BUSINESS_ERROR_CODE } from './business.error.codes';

type BusinessError = {
  errcode: string;
  errmsg: string;
};

export class BusinessException extends HttpException {
  constructor(err: BusinessError | string) {
    if (typeof err === 'string') {
      err = {
        errcode: BUSINESS_ERROR_CODE.COMMON,
        errmsg: err,
      };
    }
    super(err, HttpStatus.OK);
  }

  static throwForbidden() {
    throw new BusinessException({
      errcode: BUSINESS_ERROR_CODE.ACCESS_FORBIDDEN,
      errmsg: '抱歉哦，您无此权限！',
    });
  }
}
